﻿@model IEnumerable<IniTechCaash.WebApplication.Models.FinanceAccountDetailsViewModel>
@using IniTechCaash.Domain.Entities

@{
    ViewBag.Title = "Income Statement";

    var revenueAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Revenue);
    decimal netSales = 0.00m;
    foreach (var a in revenueAccounts)
    {
        netSales += a.Balance;
    }

    var expenseAccounts = Model.Where(a => a.Balance != 0.00m).Where(a => a.Type.GetGeneralAccountType() == GeneralAccountType.Expense);
    decimal totalExpenses = 0.00m;
    foreach (var a in expenseAccounts)
    {
        totalExpenses += a.Balance;
    }

    decimal netIncome = netSales - totalExpenses;
}

<div>
    <h3>IniTech</h3>
    <h4>Income Statement</h4>
    <p>For the Year Ended December 31, @DateTime.Now.Year</p>
</div>

<table class="table-condensed table">
    <tbody>
        <tr>
            <td colspan="2"><strong>Revenue:</strong></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < revenueAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@revenueAccounts.ElementAt(i).Name</td>
                <td></td>
                @if (i == revenueAccounts.Count() - 1)
                {
                    <td class="text-right underline">@Html.DisplayFor(m => revenueAccounts.ElementAt(i).Balance)</td>
                }
                else
                {
                    <td class="text-right">@Html.DisplayFor(m => revenueAccounts.ElementAt(i).Balance)</td>
                }
            </tr>
        }
        <tr class="active">
            <td></td>
            <td>Net Sales</td>
            <td></td>
            <td class="text-right">@netSales.ToString("N")</td>
        </tr>
        <tr>
            <td colspan="2"><strong>Expenses:</strong></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        @for (int i = 0; i < expenseAccounts.Count(); ++i)
        {
            <tr>
                <td></td>
                <td>@expenseAccounts.ElementAt(i).Name</td>
                @if (i == expenseAccounts.Count() - 1)
                {
                    <td class="text-right underline">@Html.DisplayFor(m => expenseAccounts.ElementAt(i).Balance)</td>
                }
                else
                {
                    <td class="text-right">@Html.DisplayFor(m => expenseAccounts.ElementAt(i).Balance)</td>
                }
                <td></td>
            </tr>
        }
        <tr class="active">
            <td></td>
            <td>Total Expenses</td>
            <td></td>
            <td class="text-right underline">@totalExpenses.ToString("N")</td>
        </tr>
        <tr>
            <td></td>
            <td>Net Operating Income</td>
            <td></td>
            <td class="underline text-right">@(netIncome >= 0.00m ? netIncome.ToString("N") : "(" + (netIncome * -1).ToString("N") + ")")</td>
        </tr>
        <tr>
            <td colspan="2"><strong>Net Income:</strong></td>
            <td></td>
            <td class="text-right "><span class="doubleUnderline">$@(netIncome >= 0.00m ? netIncome.ToString("N") : "(" + (netIncome * -1).ToString("N") + ")")</span></td>
        </tr>
    </tbody>
</table>

<hr />

@Html.ActionLink("Back To List", "Index")
    